#
# Sorto, the sorting game
# Copyright (C) 2012 Ralph Preston
# ralph.preston@gmail.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to:
#
# Free Software Foundation, Inc.
# 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301
# USA.
#

import time

class GameRecord(object):
    def __init__(self):
        self.seed       = None
        self.start_time = time.clock()
        self.end_time   = None
        self.counters   = []
        self.scores     = []
        self.rolls      = []
        return

    def __str__(self):
        res = 'GameRecord(scores: %s, rolls: %s)' % (self.scores, self.rolls)
        return res

    pass # End of class GameRecord

class SortoRecord(object):
    """
    The class contains a record of the play results for a player.
    """
    def __init__(self):
        self._results             = [] # list of GameRecords
        self._games_won           = 0
        self._games_won_for_round = {}
        self._total_games_played  = 0
        self._total_score         = 0
        self._total_after_last_game = 0
        self._round               = 0 # used to calculate averages
        self._total_rounds_played = 0
        self._average_rounds_per_game = 0
        self._total_rounds_after_last_game = 0
        return

    def __str__(self):
        res = 'SortoRecord(\n'
        res = '%s  results             : %s\n' % (res, [str(x) for x in self._results])
        res = '%s  total games played  : %s\n' % (res, self._total_games_played)
        res = '%s  total score         : %s\n' % (res, self._total_score)
        res = '%s  games won for round : %s\n' % (res, self._games_won_for_round)
        res = '%s)' % (res)
        return res

    def new_game(self):
        self._total_games_played += 1
        self._results.append(GameRecord())
        self._total_after_last_game = self._total_score
        self._total_rounds_after_last_game = self._total_rounds_played
        return

    def record_seed_for_game(self, game, seed):
        if game == 0 and seed is None: return
        gr = self._results[game]
        gr.seed = seed
        return

    def record_roll(self, game, roll):
        gr = self._results[game]
        gr.rolls.append(roll)
        return

    def record_score(self, game, round, score):
        if game == 0 and round == 0 and score == 0: return
        while game > len(self._results):
            self.new_game()
            pass
        gr = self._results[game]
        gr.scores.append(score)

        if score == 12:
            self._games_won += 1
            games_won_on_round = self._games_won_for_round.get(round, 0)
            self._games_won_for_round[round] = games_won_on_round + 1
            pass
        self._round = round
        return

    def record_counters_for_game(self, game, counters):
        gr = self._results[game]
        gr.counters.append(counters)
        return

    def get_games_won_on_round(self, round):
        return self._games_won_for_round.get(round, 0)

    def get_score_for_game(self, game):
        gr = self._results[game]
        return gr.scores[-1]

    def update_statistics(self):
        gr = self._results[-1]
        last_score = 0
        if len(gr.scores) > 0:
            last_score = gr.scores[-1]
            pass
        self._total_score = self._total_after_last_game + last_score
        self._total_rounds_played = self._total_rounds_after_last_game + \
            self._round
        self._average_rounds_per_game = (float(self._total_rounds_played) /
                                         float(self._total_games_played))
        return

    def get_total_score(self):
        return self._total_score
    
    def get_average_rounds_per_game(self):
        return self._average_rounds_per_game

    def number_of_wins(self):
        return self._games_won

    pass # End of class SortoRecord
